package cn.sh.library.gmwx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sh.library.gmwx.bean.PersonSearchBean;
import cn.sh.library.gmwx.dto.Pager;
import cn.sh.library.gmwx.service.PersonService;

@Controller
@RequestMapping("/cpcperson")
public class PersonController extends BaseController{
    @Resource
    private PersonService personService;

    @ResponseBody
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public Map<String, Object> search(PersonSearchBean search, Pager pager){
    	pager.setPageSize(56); // 默认查询56条
        Map<String, Object> result = new HashMap<>();
        List<Map<String, Object>> list = this.personService.list(search, pager);
     // 替换"#"符号, "党的创建和大革命时期#地方组织机构###沪江浙皖区######中共上海地委################" replace "党的创建和大革命时期-地方组织机构-沪江浙皖区-中共上海地委"
        /*for (Map<String, Object> map : list) {
        	@SuppressWarnings("unchecked")
			List<Map<String, Object>> officialEventList = (List<Map<String, Object>>) map.get("officialEvent");
        	for (Map<String, Object> officialEvent : officialEventList) {
        		String relatedOrganization = (String) officialEvent.get("relatedOrganization");
        		String relatedOrganizationNew = trimFirstAndLastChar(relatedOrganization, "#");
        		String relatedOrganizationFinal = replaceElement(relatedOrganizationNew, "#", "-");
        		officialEvent.put("relatedOrganization", relatedOrganizationFinal);
			}
		}*/
        result.put("data", list);
        result.put("pager", pager);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public Map<String, Object> info(String uri) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = this.personService.info(uri);
        @SuppressWarnings("unchecked")
        // 替换"#"符号, "党的创建和大革命时期#地方组织机构###沪江浙皖区######中共上海地委################" replace "党的创建和大革命时期-地方组织机构-沪江浙皖区-中共上海地委"
		List<Map<String, Object>> officialEventList =  (List<Map<String, Object>>) map.get("officialEvent");
        for (Map<String, Object> officialEvent : officialEventList) {
    		String relatedOrganization = (String) officialEvent.get("relatedOrganization");
    		String relatedOrganizationNew = trimFirstAndLastChar(relatedOrganization, "#");
    		String relatedOrganizationFinal = replaceElement(relatedOrganizationNew, "#", "-");
    		officialEvent.put("relatedOrganization", relatedOrganizationFinal);
		}
        result.put("data", map);
        return result;
    }
    
    /**
    * 去除首尾指定字符
    * @param string 字符串
    * @param element 指定字符
    */
    private static String trimFirstAndLastChar(String string, String element){
        boolean beginIndexFlag = true;
        boolean endIndexFlag = true;
        do{
            int beginIndex = string.indexOf(element) == 0 ? 1 : 0;
            int endIndex = string.lastIndexOf(element) + 1 == string.length() ? string.lastIndexOf(element) : string.length();
            string = string.substring(beginIndex, endIndex);
            beginIndexFlag = (string.indexOf(element) == 0);
            endIndexFlag = (string.lastIndexOf(element) + 1 == string.length());
        } while (beginIndexFlag || endIndexFlag);
        return string;
    }
    
    /**
     * 替换指定字符
     * @param string 字符串
     * @param elementOld 旧指定字符
     * @param elementNew 新指定字符
     */
     private static String replaceElement(String string, String elementOld, String elementNew){
    	 String regEx = "['"+ elementOld + "']+"; // 一个或多个#
    	 Pattern pattern = Pattern.compile(regEx);
    	 Matcher matcher = pattern.matcher(string);//这里把想要替换的字符串传进来
    	 String newString = matcher.replaceAll(elementNew).trim();
    	 return newString;
     }
     
}
